<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>管理员</title>
		<script src="js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="root">
			<span>新增抽奖物品:</span>&nbsp;<input type="text" name="adgodod" id="addgood" value="" />
			<span>抽奖物品数量:</span>&nbsp;<input type="number" name="addgoodcount" id="addgoodcount" value="" />
			&nbsp;&nbsp;&nbsp;
			<input type="button" name="addgoodbtn" id="addgoodbtn" value="新增" /><br /><br />
			<span>当前抽奖项</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="good" readonly="readonly" />
			&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" name="random" id="random" value="抽奖" />
			<br /><br />
			<span>设置当前抽奖项:</span>&nbsp;&nbsp;<select id="goodlist"></select>&nbsp;&nbsp;&nbsp;<input type="button" id="setgood"
			 value="设置" /><br /><br />
			<h2 id="result"></h2>
			<script type="text/javascript">
				var BaseUrl = "http://127.0.0.1:8090/";
				//获得当前抽奖项
				var getGood = function() {
					$.ajax({
						url: BaseUrl+"getGood",
						success: function(data) {
							if (data.status == 200) {
								var good = data.data;
								$("#good").attr("goodid", good.id);
								$("#good").val(good.name);
							} else {
								alert(data.msg);
								$("#good").val("");
							}
						}
					});
				}
				//获得抽奖项列表
				var getAllGoods = function() {
					$("#goodlist").empty();
					$.ajax({
						url: BaseUrl+"getAllGoods",
						success: function(data) {
							var goodlist = data.data;
							for (var i = 0; i < goodlist.length; i++) {
								var good = goodlist[i]
								var ele = "<option value='" + good.id + "'>" + good.name + "</option>";
								$("#goodlist").append(ele);
							}
						}
					})
				}
				//添加物品
				var addgood = function() {
					var name = $("#addgood").val().trim();
					var count = $("#addgoodcount").val();
					$.ajax({
						url: BaseUrl+"insertGood",
						type: "POST",
						data: {
							"name": name,
							"num": count
						},
						success: function(data) {
							if (data.status == 200) {
								alert("新增成功");
								getAllGoods();
								$("#addgood").val("");
								$("addgoodcount").val("");
							}
						}
					})
				};
				//抽奖
				var random = function() {
					$.ajax({
						url: BaseUrl+"randomPerson",
						success: function(data) {
							console.log(data.data);
							if (data.status == 200) {
								var list = data.data;
								var text = "中奖的qq号为:  ";
								for(i=0;i<list.length;i++){
									text += list[i].qq + "，";
								}
								$("#result").text(text);
							} else {
								alert(data.msg);
							}
						}
					})
				}
				//设置抽奖项目
				var setGood = function() {
					var id = $("#goodlist").val();
					$.ajax({
						url: BaseUrl+"setGood",
						type: "post",
						data: {
							"id": id
						},
						success: function(data) {
							if (data.status == 200) {
								alert("设置成功");
								getGood();
							} else {
								alert("设置失败");
							}
						}
					})
				}
				//新增按钮点击事件
				$("#addgoodbtn").on("click", addgood);
				//抽奖按钮点击事件
				$("#random").on("click", random);
				//设置按钮点击事件
				$("#setgood").on("click", setGood);
				getGood();
				getAllGoods();
			</script>
		</div>
	</body>
</html>
